package View;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import Model.Comparison;
import classes.DocPair;

public class optionGui implements ActionListener {
	private JFrame frame;
	private JPanel panel;
	private JButton compare;
	private JButton enterString;
	private JLabel label;
	private processing processGui;
	private ResultGui result;
	private StringGui strGui;
	
	public optionGui(processing process){
		processGui = process;
		result = null;
		frame = new JFrame();
		panel = new JPanel();
		compare = new JButton();
		enterString = new JButton();
		label = new JLabel();
		compare.setText("Start Comparison");
		enterString.setText("String comparison");
		panel.add(compare);
		panel.add(enterString);
		label.setText("Paste all files in the folder specified in menue and select");
		frame.add(label,BorderLayout.NORTH);
		frame.add(panel,BorderLayout.CENTER);
		frame.setBounds(50,50,500,100);
		strGui = new StringGui();
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	public void setVisible(){
		frame.setVisible(true);
	}
	public void closeVisible(){
		frame.setVisible(false);
	}
	public void register(){
		compare.addActionListener(this);
		enterString.addActionListener(this);
	}
	public void actionPerformed(ActionEvent event) {
		String cmd = event.getActionCommand();
		if(cmd.equals("Start Comparison")){
			closeVisible();
			processGui.openVisible();
			Comparison cp = new Comparison();
			try {
				cp.filesRead(Comparison.path);
				int n = cp.data.length;
				//Calculating number of documents array
				double lenTemp = Math.ceil( ( n * (n-1) ) / 2 );
				int len = (int)lenTemp;
				cp.pairs = new DocPair[len];
				File directory = new File(Comparison.path);
				File[] files = directory.listFiles();
				int fileIndex = files.length;
				int pairIndex = 0;
				for(int i = 0; i < fileIndex;i++){
					for(int j = (i+1); j < fileIndex;j++){
						cp.pairs[pairIndex] = new DocPair();
						cp.pairs[pairIndex].setFirstName(files[i].getName());
						cp.pairs[pairIndex].setSecondName(files[j].getName());
						cp.pairs[pairIndex].setTotalWordsFirst(cp.countWords( files[i].getAbsolutePath()));
						cp.pairs[pairIndex].setTotalWordsSecond(cp.countWords( files[j].getAbsolutePath()));
						cp.pairs[pairIndex].setSimilarWordsCount(cp.countSimilarword(files[i].getAbsolutePath(), files[j].getAbsolutePath()));
						cp.pairs[pairIndex].calculateSimilarityRate();
						pairIndex++;
					}
				}
				result = new ResultGui();
				//result.frame.setContentPane(result.scroll);
				result.panel.setLayout(new GridLayout(cp.pairs.length+1,1));
				result.panel.add(new JLabel("Names"));
				result.panel.add(new JLabel("Similarity"));
				result.panel.add(new JLabel("View"));
				for(int i = 0; i < cp.pairs.length;i++){
					String firstCol = cp.pairs[i].getFirstName()+"-with-"+cp.pairs[i].getSecondName();
					double secCol = cp.pairs[i].getSimilarityRate();
					result.panel.add(new JLabel(firstCol));
					result.panel.add(new JLabel(secCol+""));
					result.panel.add(new JButton("View"));
				}
				result.frame.add(result.panel);
				processGui.closeVisible();
				result.setVisible();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if(cmd.equals("String comparison")){
			closeVisible();
			strGui.setVisible();
		}
	}
}
